<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  title="送检"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"

)
  mt-form(
      v-bind="editFormProps"
      style="margin-top: 10px;"
    )
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import {
  type FormItemProps,
  type TableProps,
  useNotify,
  type EditFormProps,
} from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { request, loadingMap } = useRequest({
  batchGenerateSendInspection: (q) => {
    return $api.lis.specimen
      .batchGenerateSendInspection(
        q.specimenList,
        q.sendInspectionId,
        q.numberPeople
      )
      .then(() => {
        $notify.success("送检成功");
        emit("message-to-parent");
        appmodel.value = false;
      });
  },
});

const updateConfirmModel = () => {
  if (!editingObj?.value?.sendInspectionId) {
    $notify.error("请选择送检医生");
    return;
  }
  request("batchGenerateSendInspection", {
    specimenList: props.selectionRow,
    sendInspectionId: editingObj?.value?.sendInspectionId,
    numberPeople: props.selectionRow.length,
  });
};

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "sendInspectionId",
      label: "送检医生:",
      required: true,
      as: "select",
      options: {
        loadMethod: () => $api.user.listDoctorAsOptions({ officeId: "" }),
      },
    },
    {
      prop: "quantity",
      label: "送检数量:",
      required: false,
      disabled: true,
    },
  ];

  const editProps: EditFormProps = {
    items: formItems,
    model: editingObj.value,
    columns: 1,
  };
  return editProps;
});

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      editingObj.value = {};
      editingObj.value = {
        quantity: props.selectionRow.length,
      };
    }
  },
  {
    immediate: true,
  }
);
</script>
